package com.ms.service.module.fish.test;

import java.util.List;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.ms.service.module.fish.message.FishMsgType;
import com.ms.service.module.fish.message.FishRequestWrap;

public class ClientRobot {
	
	private static Logger logger = LoggerFactory.getLogger(ClientRobot.class);
	
	public static String userId;
	public static String userName;
	public static String nickName;
	public static long coin;
	public static int desk;
	public static int deskIndex;
	public static int battery;
	public static int rate;
	
	public static List<Integer> fishs = Lists.newArrayList();
	
	static int scriptId;

	public static void action100(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		userId = dataWrap.getString();
		String userName = dataWrap.getString();
		logger.info("userId:{},userName:{}",userId,userName);
		coin = dataWrap.getLong();
		nickName = dataWrap.getString();
		
	}

	public static void action111(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		desk = dataWrap.getInt();dataWrap.getString();dataWrap.getInt();
		int len = dataWrap.getInt();
		for(int i=0;i<len;i++){
			byte ind = dataWrap.getByte();
			String uid = dataWrap.getString();
			dataWrap.getLong();
			if(uid.equals(userId)){
				deskIndex = ind;
				battery = dataWrap.getByte();
				rate = dataWrap.getInt();
			}else{
				dataWrap.getByte();
				dataWrap.getInt();
			}
			dataWrap.getString();
			dataWrap.getInt();
		}
		
		// 进入游戏后,测试退出游戏
		FishClient.sendMsg(ctx,new ClientMsgWriter(FishMsgType.ACTION_118));
	}

	public static void action112(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		logger.info("有玩家加入：{}-{}",dataWrap.getByte(),dataWrap.getInt());
	}
	
	public static void action118(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		logger.info("玩家退出游戏：{}",dataWrap.getInt());
		
		// 重新登录
	}

	public static void action201(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		int scriptId = dataWrap.getInt();
		logger.info("有鱼进入：{}-{}",scriptId,dataWrap.getInt());
		fishs.add(scriptId);
		
		// 进入第-个鱼开始捕鱼
		FishClient.sendMsg(ctx,new ClientMsgWriter(FishMsgType.ACTION_202,scriptId));
	}

	public static void action202(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		String uid = dataWrap.getString();
		int batteryT = dataWrap.getByte();
		int scriptId = dataWrap.getInt();
		int winCoin = dataWrap.getInt();
		int wbl = dataWrap.getInt();
		if(winCoin > 0){
			logger.info("捕鱼成功：{},{},{},{}",new Object[]{uid,scriptId,winCoin,wbl});
		}else{
			logger.info("捕鱼失败：{},{},{},{}",new Object[]{uid,scriptId,winCoin,wbl});
			// 继续捕鱼
//			ctx.writeAndFlush(new ClientMsgWriter(FishMsgType.ACTION_202,scriptId));
		}
	}

	public static void action203(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		String uid = dataWrap.getString();
		byte batteryT = dataWrap.getByte();
		if(uid.equals(userId)){
			battery = batteryT;
		}
		logger.info("切换炮台：{}-{}",uid,batteryT);
	}

	public static void action204(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		String uid = dataWrap.getString();
		int rateT = dataWrap.getInt();
		if(uid.equals(userId)){
			rate = rateT;
		}
		logger.info("调节炮台倍率：{}-{}",uid,rateT);
	}

	public static void action113(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		int fscr = dataWrap.getInt();
		int palyTime = dataWrap.getInt();
		int size = dataWrap.getInt();
		int scriptId = 0;
		for(int i=0;i<size;i++){
			scriptId = dataWrap.getInt();
		}
		logger.info("同步游戏：{}-{}，{}，{}",new Object[]{fscr,palyTime,size,scriptId});
	}
	
	public static void action205(final ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		scriptId = dataWrap.getInt();
		int times = dataWrap.getInt();
		logger.info("开始新一轮游戏：{}-{}",scriptId,times);
//		TimerManager.schedule(()->{
//			FishClient.sendMsg(ctx,new ClientMsgWriter(FishMsgType.ACTION_200,1,1));
//			return null;
//		}, 3000);
	}
	
	public static void action200(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		String uid = dataWrap.getString();
		int battery = dataWrap.getByte();
		logger.info("发出炮弹：{}-{}",uid,battery);
		FishClient.sendMsg(ctx,new ClientMsgWriter(FishMsgType.ACTION_202,battery,scriptId));
	}

	public static void action102(ChannelHandlerContext ctx, FishRequestWrap dataWrap) {
		// 桌台信息
		int size = dataWrap.getInt();
		logger.info("size:{}",size);
		int len = dataWrap.getInt();
		for(int i=0;i<len;i++){
			int desk = dataWrap.getInt();
			dataWrap.getByte();
			
			int lenj = dataWrap.getInt();
			if(lenj > 0){
				for(int j=0;j<lenj;j++){
					int index = dataWrap.getByte();
					String userId = dataWrap.getString();
					byte status = dataWrap.getByte();
					dataWrap.getInt();
					logger.info("有人:{}-{}-{}-{}",new Object[]{ desk,index,userId,status});
				}
			}
			
		}
		
		// 登录后进入游戏
		FishClient.sendMsg(ctx, new ClientMsgWriter(FishMsgType.ACTION_111,11,deskIndex));
	}
	
	
	
	
}
